﻿using DataConnector;
using LabManager.UserControls;
using LabManager.UserControls.TabControls;
using LabManager.UserControls.Tabs;
using LabManager.UserControls.Tabs.Masterdata;
using LabManager.UserControls.Tabs.Products;
using LabManager.UserControls.Tabs.Templates;
using Library;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LabManager
{
    public partial class MainForm : Form
    {
        private TabTemplate tabUser = new TabUser() { Dock = DockStyle.Fill };
        private TabTemplate tabDepartments = new TabDepartments() { Dock = DockStyle.Fill };
        private TabTemplate tabUserGroups = new TabUserGroups() { Dock = DockStyle.Fill };
        private TabTemplate tabEnvironment = new TabEnvironment() { Dock = DockStyle.Fill };
        private TabTemplate tabManufacturer = new TabManufacturer() { Dock = DockStyle.Fill };
        private TabTemplate tabProductCategory = new TabProductCategory() { Dock = DockStyle.Fill };
        private TabTemplate tabMachineType = new TabMachineType() { Dock = DockStyle.Fill };
        private TabTemplate tabMachine = new TabMachine() { Dock = DockStyle.Fill };
        private TabTemplate tabState = new TabState() { Dock = DockStyle.Fill };
        private TabTemplate tabProductType = new TabProductType() { Dock = DockStyle.Fill };
        private TabTemplate tabPackageInsert = new TabPackageInsert() { Dock = DockStyle.Fill };
        private TabProducts tabProducts = new TabProducts() { Dock = DockStyle.Fill };
        private TabProductObject tabProductObject = new TabProductObject() { Dock = DockStyle.Fill };

        private RibbonButton selectedButton = new RibbonButton();
       // private User user;

        public MainForm()
        {
            InitializeComponent();
            SetLanguage();
            SetTexts();
        }

        #region Events

        private void tbUserdata_Click(object sender, EventArgs e)
        {
            panelGround.Controls.Clear();

            var userData = new Userdata();
            userData.Dock = DockStyle.Fill;
            panelGround.Controls.Add(userData);
        }

        private void tsInfrastructure_Click(object sender, EventArgs e)
        {
            panelGround.Controls.Clear();

            var infrastructure = new Infrastructure(); //TODO: Add dynamically
            infrastructure.Dock = DockStyle.Fill;
            panelGround.Controls.Add(infrastructure);
        }

        private void rbUsers_Click(object sender, EventArgs e)
        {
            selectedButton.Checked = false;
            panelHolder.Controls.Clear();
            tabUser.Load();
            panelHolder.Controls.Add(tabUser);
            selectedButton = sender as RibbonButton;
        }

        private void rbDepartments_Click(object sender, EventArgs e)
        {
            selectedButton.Checked = false;
            panelHolder.Controls.Clear();
            tabDepartments.Load();
            panelHolder.Controls.Add(tabDepartments);
            selectedButton = sender as RibbonButton;
        }

        private void rbUsergroups_Click(object sender, EventArgs e)
        {
            selectedButton.Checked = false;
            panelHolder.Controls.Clear();
            tabUserGroups.Load();
            panelHolder.Controls.Add(tabUserGroups);
            selectedButton = sender as RibbonButton;
        }

        private void rbLabEnvironment_Click(object sender, EventArgs e)
        {
            selectedButton.Checked = false;
            panelHolder.Controls.Clear();
            tabEnvironment.Load();
            panelHolder.Controls.Add(tabEnvironment);
            selectedButton = sender as RibbonButton;
        }

        private void rbManufacturer_Click(object sender, EventArgs e)
        {
            selectedButton.Checked = false;
            panelHolder.Controls.Clear();
            tabManufacturer.Load();
            panelHolder.Controls.Add(tabManufacturer);
            selectedButton = sender as RibbonButton;
        }


        private void rbProductCategory_Click(object sender, EventArgs e)
        {
            selectedButton.Checked = false;
            panelHolder.Controls.Clear();
            tabProductCategory.Load();
            panelHolder.Controls.Add(tabProductCategory);
            selectedButton = sender as RibbonButton;
        }

        private void rbMachineType_Click(object sender, EventArgs e)
        {
            selectedButton.Checked = false;
            panelHolder.Controls.Clear();
            tabMachineType.Load();
            panelHolder.Controls.Add(tabMachineType);
            selectedButton = sender as RibbonButton;
        }

        private void rbMachine_Click(object sender, EventArgs e)
        {
            selectedButton.Checked = false;
            panelHolder.Controls.Clear();
            tabMachine.Load();
            panelHolder.Controls.Add(tabMachine);
            selectedButton = sender as RibbonButton;
        }

        private void rbProductType_Click(object sender, EventArgs e)
        {
            selectedButton.Checked = false;
            panelHolder.Controls.Clear();
            tabProductType.Load();
            panelHolder.Controls.Add(tabProductType);
            selectedButton = sender as RibbonButton;
        }

        private void rbProducts_Click(object sender, EventArgs e)
        {
            selectedButton.Checked = false;
            panelHolder.Controls.Clear();
            tabProducts.Load();
            panelHolder.Controls.Add(tabProducts);
            selectedButton = sender as RibbonButton;
        }

        private void rbPackageInserts_Click(object sender, EventArgs e)
        {
            selectedButton.Checked = false;
            panelHolder.Controls.Clear();
            tabPackageInsert.Load();
            panelHolder.Controls.Add(tabPackageInsert);
            selectedButton = sender as RibbonButton;
        }

        private void rbProductObjects_Click(object sender, EventArgs e)
        {
            selectedButton.Checked = false;
            panelHolder.Controls.Clear();
            tabProductObject.Load();
            panelHolder.Controls.Add(tabProductObject);
            selectedButton = sender as RibbonButton;
        }

        private void rmLogin_Click(object sender, EventArgs e)
        {
            var loginForm = new LoginForm();
            loginForm.ShowDialog();

            var user = loginForm.User;

            if (user != null)
            {
                MessageBox.Show(string.Format("Welcome {0} {1} ({2})", user.FirstName, user.LastName, user.Login));
            }

            rmLogin.Text = "Logout";
        }

        #endregion

        #region Helper

        private void LoadMenuByUserGroup(UserGroup userGroup)
        {
            switch (userGroup.UserGroupName)
            { 
                case Enums.UserGroup.POWERUSER:
                    // TODO
                    break;
            }
        }

        private void SetLanguage()
        {
            string language = ConfigurationManager.AppSettings[Helper.LANGUAGE];
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language);
        }

        private void SetTexts()
        {
            // Todo
        }

        #endregion


    }
}
